package com.team.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.team.entity.User;
import com.team.util.DbHelp;
import com.team.util.ResultSetPackge;

public class UserDao {
	
	DbHelp<User> db = new DbHelp<User>();
	
	/**
	 *
	 * @param user
	 * @return
	 */
	public int  saveUser(User user){
		String sql = "insert into t_user(id,userName,password,pic)values(?,?,?,?) ";
		return db.executeUpdateSql(sql, user.getId(),user.getUsername(),user.getPassword(),user.getPic());
		
	}
	
	public User selectById(int id){
		String sql = "select id,username,password,pic from t_user where id=?";
		return db.exeSelectForObject(sql, new PackgeUser(),id);
	}
	/**
	 * 
	 * @param name
	 * @param pwd
	 * @return
	 */
	public User selectByNameAndPwd(String name,String pwd){
		String sql = "select id,username,password,pic from t_user where username=? and password=?";
		return db.exeSelectForObject(sql, new PackgeUser(), name,pwd);
	}
	
	/**
	 *
	 * @param name
	 * @return
	 */
	public User selectByName(String name){
		String sql = "select id ,username ,password,pic from t_user where username = ?";
		return db.exeSelectForObject(sql,  new PackgeUser() , name);
	}
	
	public List<User> selectListByName(String name){
		String sql = "select id ,username ,password,pic from t_user where username = ?";
		return db.exeSelectForList(sql, new PackgeUser() , name);
	}
	
	private class PackgeUser implements ResultSetPackge<User>{

		public User packgeResultSet(ResultSet rs) throws SQLException {
				User user = new User();
				user.setId(rs.getInt("id"));
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
				user.setPic(rs.getString("pic"));
				return user;
				
		}
		
	}
}
